package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-18
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member) {
        System.out.println("111111"+MD5.encrypt("111111"));
        //调用实现  jwt的方式生产出来token
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }
    //注册
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token 获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类 根据request 对象获取 头信息 返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据的用户信息
        UcenterMember member = memberService.getById(memberId);
        //返回信息
        return R.ok().data("userInfo",member);

    }
    //根据用户id查询用户信息（远程调用）  在公用  UcenterMemberOrder 才最简洁
    @PostMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = memberService.getById(id);

        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();

        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }
    //查询某一天注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }



}

